/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Entidad.Tarea;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author user
 */
public class TareaDAO {
    
    private static EntityManagerFactory emf=Persistence.createEntityManagerFactory("CRMPU");



    public boolean crear(Tarea object) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean salida=false;
        try {
            em.persist(object);
            em.getTransaction().commit();
            salida = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return salida;
        }
    }

     public boolean eliminar(Tarea object) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Tarea tar = em.find(Tarea.class, object.getID());
        boolean ret = false;
        try {
            em.remove(tar);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }

    public Tarea leer(Tarea par) {

        EntityManager em = emf.createEntityManager();
        Tarea tarea = null;
        Query q = em.createQuery("SELECT t from Tarea t " +
                "WHERE t.ID LIKE :ID")
                .setParameter("ID", par.getID());
        try {
            tarea = (Tarea) q.getSingleResult();
        } catch (NonUniqueResultException e) {
            tarea = (Tarea) q.getResultList().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            return tarea;
        }
    }

  
    public boolean actualizar(Tarea object, Tarea nuevoObjeto) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean ret = false;
        try {
            object = leer(object);
            object.setID(nuevoObjeto.getID());
            object.setFecha_in(nuevoObjeto.getFecha_in());
            object.setFecha_fin(nuevoObjeto.getFecha_fin());
            object.setTipo(nuevoObjeto.getTipo());
            object.setDescripcion(nuevoObjeto.getDescripcion());
            object.setEstado(nuevoObjeto.getEstado());
            object.setCompra(nuevoObjeto.getCompra());
            
            em.merge(object);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }


    
}
